package com.tedu.charging.service.device.canal;

import com.alibaba.fastjson2.JSONObject;
import com.tedu.charging.service.device.constant.RedisConstants;
import com.tedu.charging.service.device.dao.GunInfoMapper;
import com.tedu.charging.service.device.pojo.po.ChargingStationCanalPO;
import com.tedu.charging.service.device.pojo.po.ChargingStationPO;
import com.tedu.charging.service.device.pojo.po.GunInfoPO;
import com.tedu.charging.service.device.pojo.vo.ChargingStationVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

import java.util.List;

@CanalTable("charging_station")  //需要监听的表
@Component
public class StationHandler implements EntryHandler<ChargingStationCanalPO> {//指定表关系实体类
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    GunInfoMapper gunInfoMapper;

    @Override
    public void insert(ChargingStationCanalPO historyLog) {
        //新增数据时执行此方法
        System.out.println("insert：" + JSONObject.toJSONString(historyLog));
    }

    @Override
    public void update(ChargingStationCanalPO before, ChargingStationCanalPO after) {
        //更新数据时执行此方法
        System.out.println("update before：" + JSONObject.toJSONString(before));
        System.out.println("update after：" + JSONObject.toJSONString(after));
        Integer id = after.getId();
        ChargingStationVO chargingStationVO = new ChargingStationVO();
        BeanUtils.copyProperties(after, chargingStationVO);
        BoundHashOperations boundHashOperations = redisTemplate.boundHashOps(RedisConstants.STATION_DETAIL_HASH);

        //查询充电站的充电枪信息
        List<GunInfoPO> gunInfoPOList = gunInfoMapper.selectByStationId(id);
        chargingStationVO.setGunInfoPOList(gunInfoPOList);


        boundHashOperations.put(id.toString(), chargingStationVO);

    }

    @Override
    public void delete(ChargingStationCanalPO historyLog) {
        //删除数据时执行此方法
        System.out.println("delete：" + JSONObject.toJSONString(historyLog));
    }
}
